<?php
/** 留言板
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3 0003
 * Time: 14:31
 */

//导入
include 'mysql.php';
$mysql = new mysql();
//留言内容查询
//$sql = "select * from message where sn = 0 and p_id = ".$_SESSION['id'].' or g_id = '.$_SESSION['id'];
$sql = 'select * from message where p_id = '.$_SESSION["id"].' or g_id = '.$_SESSION["id"].' and sn = 0 order by time desc;';
$data = $mysql->select_all($sql);
//重组数组，将发送人和接收人装入
foreach($data as $i => $j){
    $aql1 = "select username from test_table where id = ".$j['p_id'];
    $user1 = $mysql->select_one($aql1);
    $data[$i]['put_username'] = $user1['username'];

    $aql2 = "select username from test_table where id = ".$j['g_id'];
    $user2 = $mysql->select_one($aql2);
    $data[$i]['get_username'] = $user2['username'];
}

//修改
if (isset($_REQUEST['update1'])) {
    if (!empty($_POST)) {
        $_SESSION['up_id'] = $_POST['id'];
        header("Refresh: 0; url=update.php");
    }
}

//删除
if (isset($_REQUEST['delete'])) {
    if (!empty($_POST)) {
        $id = $_POST['id'];
        $table = 'message';
        $where = "id = $id";
        //"DELETE FROM `test`.`message` WHERE  `id` = $id;";
        $ints = $mysql->delete($table, $where);
        if ($ints == true) {
            alert("删除成功。", 0, 'message.php');
        } else {
            alert("删除失败。", 0, 'message.php');
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        input{
            width: 50px;
            height: 25px;
        }
        button{
            width: 100px;
            height: 30px;
        }
        #head{
            width: 100%;
            height: 50px;
        }
        #head_son{
            margin: 0 auto;
            height: 50px;
            text-align: center;
        }
        #head_son span{
            font-size: 40px;
            color: deepskyblue;
        }
        #left{
            float: left;
            width: 843px;
            margin: 20px 50px;
            padding-left: 350px;
        }
        #box_left{
            padding: 10px;
            float: left;
            border: 1px solid cyan;
        }
        #box_right{
            padding: 10px;
            float: right;
            border: 1px solid cyan;
        }
        #right{
            float: right;
        }
        #text{
            padding: 5px;
            border: 1px solid #58a4b0;
        }
        #text img{
            min-width: 50px;
            max-width: 378px;
            transition: transform 2s;
            border: 1px;
            z-index: 1;
        }
        #text img:hover{
            transform: scale(2);
            box-shadow: 10px 10px 15px #ccc;
            z-index: 2;
        }
    </style>
</head>
<body>

<div id="head">
    <div id="head_son">
            <span>欢迎：
                <?php
                    echo $_SESSION["username"];
                ?>
            </span>
    </div>
</div>

<div id="left">
    <h1>留言内容</h1>
    <?php
    foreach($data as $key => $value){
        if($_SESSION["id"] == $value['p_id']){  ?>
        <div id="box_right">
        <?php   }else{  ?>
        <div id="box_left">
        <?php    }  ?>
            <form method="post">
                <span>发送人：</span><?php echo $value['put_username']; ?><br>
                <span>接收人：</span><?php echo $value['get_username']; ?><br>
                <div style="width: 400px;color: black"><span>时间：</span><?php echo $value['time']; ?></div>
                <span>内容：</span><br>
                <div id="text"><?php echo $value['text']; ?></div>
                <input type="hidden" name="id" value="<?php echo $value['id']; ?>"><br>
                <input type="submit" value="删除" name="delete" onclick="return on_sub1()">
                <?php   if($_SESSION["id"] == $value['p_id']){  ?>
                    <input type="submit" value="修改" name="update1" onclick="return on_sub2()">
                <?php   }   ?>
            </form>
        </div>
    <?php   };  ?>
</div>

    <div id="right">
        <button onclick="bti()">发送留言</button><br><br>
        <button onclick="btn()">退出登录</button>
    </div>
</body>
</html>
<script type="text/javascript">
    function on_sub2() {
        window.location.href="update.php";
    }
    function btn() {
        var mymessage=confirm("确定要退出登录吗？");
        if(mymessage == true){
            window.location.href="login.php";
            return true;
        }else {
            window.location.href="message.php";
            return false;
        }
    }
    function bti(){
        window.location.href="insert.php";
    }
</script>